<?php

/**
 * Merging a GotProRole's information
 * @package ModuleGestionDocuments
 * @version 1.0b
 * @var $_GET['model'] The model's ID
 * @var $_GET['gotprorole'] The gotprorole's ID
 * @return merged .odt
 */

session_start();

// Configuration file
include '../configuration.php';
// Functions
include '../inc/functions.php';

// Checking if user is logged in
if (!CheckSession()):
	exit();
endif;

// Include all objects
// TODO: Include only needed classes
foreach (glob($configuration['webroot'].'objects/*.php') as $class) { include_once $class; }

// odtPHP library
include '../odt/odf.php';

// Loading model
$model = new Model();
$model->Get($_GET['model']);
$file = $configuration['webroot'].'data/models/'.$model->modelId.'.'.$model->extension;
$odf = new odf($file);

// Loading gotprorole
$gotprorole = new GotProRole();
$gotprorole->Get($_GET['gotprorole']);
$company = $gotprorole->GetCompany();
$address = $company->GetAddressList(Array(Array('type', '=', 'MAINADDRESS')));
$address = $address[0];
$companycontact = $company->GetContact();
$contact = $gotprorole->GetContact();
$date = $dateC = $dateFrom = $dateTo = '';
if (strlen($contact->birthdate) > 0):
	$dateC = explode('-', $companycontact->birthdate);
	$dateC = $date[2].'/'.$date[1].'/'.$date[0];
endif;
if (strlen($contact->birthdate) > 0):
	$date = explode('-', $contact->birthdate);
	$date = $date[2].'/'.$date[1].'/'.$date[0];
endif;
$dateFrom = explode('-', $gotprorole->from);
$dateFrom = $dateFrom[2].'/'.$dateFrom[1].'/'.$dateFrom[0];
$dateTo = explode('-', $gotprorole->to);
$dateTo = $dateTo[2].'/'.$dateTo[1].'/'.$dateTo[0];
$prorole = $gotprorole->GetProrole();

// Merging
try { $odf->setVars('contact_salutation', $contact->salutation); }
catch (Exception $e) {}
try { $odf->setVars('contact_lastname', $contact->lastname); }
catch (Exception $e) {}
try { $odf->setVars('contact_firstname', $contact->firstname); }
catch (Exception $e) {}
try { $odf->setVars('contact_birthdate', $date); }
catch (Exception $e) {}
try { $odf->setVars('contact_jobtitle', $contact->jobtitle); }
catch (Exception $e) {}
try { $odf->setVars('contact_email', $contact->email); }
catch (Exception $e) {}
try { $odf->setVars('company_name', $company->name); }
catch (Exception $e) {}
try { $odf->setVars('address_number', $address->number); }
catch (Exception $e) {}
try { $odf->setVars('address_street', $address->street); }
catch (Exception $e) {}
try { $odf->setVars('address_building', $address->building); }
catch (Exception $e) {}
try { $odf->setVars('address_city', $address->city); }
catch (Exception $e) {}
try { $odf->setVars('address_postalcode', $address->postalcode); }
catch (Exception $e) {}
try { $odf->setVars('companycontact_salutation', $companycontact->salutation); }
catch (Exception $e) {}
try { $odf->setVars('companycontact_lastname', $companycontact->lastname); }
catch (Exception $e) {}
try { $odf->setVars('companycontact_firstname', $companycontact->firstname); }
catch (Exception $e) {}
try { $odf->setVars('companycontact_birthdate', $dateC); }
catch (Exception $e) {}
try { $odf->setVars('companycontact_jobtitle', $companycontact->jobtitle); }
catch (Exception $e) {}
try { $odf->setVars('companycontact_email', $companycontact->email); }
catch (Exception $e) {}
try { $odf->setVars('prorole', $prorole->name); }
catch (Exception $e) {}
try { $odf->setVars('date_from', $dateFrom); }
catch (Exception $e) {}
try { $odf->setVars('date_to', $dateTo); }
catch (Exception $e) {}

// Exporting merged .odt for downloading
$odf->exportAsAttachedFile();
?>
